 /**
 * 逻辑、功能相关描述:
 *
 * @author yandawei 编写日期：2008-10-13
 */
package com.ce.pms.common.aop.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.ce.pms.common.annotation.BuildingParameter;
import com.ce.pms.common.dao.IDataRightDao;
import com.ce.pms.exception.DaclUnsupportedParameterException;
import com.ce.pms.house.model.BuildingDomain;
import com.ce.util.CurrentUserThread;

/**
 * @author yandawei
 *
 */
public aspect BuildingParameterAdvice {
	private IDataRightDao dataRightDao;



	public IDataRightDao getDataRightDao() {
		return dataRightDao;
	}

	public void setDataRightDao(IDataRightDao dataRightDao) {
		this.dataRightDao = dataRightDao;
	}

	private Logger log =  Logger.getLogger(BuildingParameterAdvice.class);

	pointcut buildingParameterPointcut(BuildingParameter anno):execution(* *(..))&& @annotation(anno);

	before(BuildingParameter anno):buildingParameterPointcut(anno) && !within(BuildingParameterAspect){

		log.info("===DACL===");
		Long userId = CurrentUserThread.get().getUserId();
		int index = anno.index();
		Object[] args = thisJoinPoint.getArgs();
		Object obj = args[index];
		Long buildingId = null;
		if(obj instanceof Long){
			buildingId = (Long)obj;
		}else if(obj instanceof BuildingDomain){
			buildingId = ((BuildingDomain)obj).getKeyword();
		}else{
			throw new DaclUnsupportedParameterException("不支持除Long,BuildingDomain以外的参数!");
		}
		System.out.println("检验访问主键为"+buildingId.toString()+"记录权限...");
		String sql = "select count(*) from SecUserRelationRight secRight where secRight.typeCode='002' and secRight.resourceId= ? and secRight.userId= ?";
		List list = dataRightDao.findAllResult(sql, new Long[]{buildingId,userId});
		if(list.size() <=0){
			throw new DaclUnsupportedParameterException("数据权限不足,不能访问!");
		}
	}
}
